package com.qst.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.*;
import com.qst.pojo.Class;
import com.qst.query.ClassQuery;
import com.qst.query.EmployeeQuery;
import com.qst.query.UserQuery;
import com.qst.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @ClassNmae: ClassController
 * @Author: 邓嘉顺
 * @Description:
 */
@Controller
@RequestMapping("class")
public class ClassController {
    @Autowired
    ClassService classService;

    @RequestMapping("/ToUserClassData")
    @ResponseBody
    public List<Class> ToUserClassData(ClassQuery classQuery, Model model) {
        List<Class> courseList = classService.getCourseList(classQuery);
        return courseList;
    }


    @RequestMapping("/ToUserClass")
    public String ToUserClass(ClassQuery classQuery, Model model) throws JsonProcessingException {
        List<Class> courseList = classService.getCourseList(classQuery);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(courseList);
        model.addAttribute("courseList", json);
        return "userClass";
    }


    @RequestMapping("/AddChoose")
    @ResponseBody
    @Transactional
    public Result choose(Integer id, HttpSession session, Model model) throws JsonProcessingException {
//        String realVerityCode = (String) session.getAttribute("verifyCode");

        User userInfo = (User) session.getAttribute("userInfo");
        Integer userid = userInfo.getId();
//        Class aClass = classService.queryAllById(id);
//        Integer classHour = aClass.getClassHour();
//        Integer cardHour = userInfo.getCardHour();
//        Integer nextHour=cardHour-classHour;
//        if(nextHour>=0){
//            classService.updateNextHour(nextHour,userid);
//        }


        Choose choose = new Choose();
        choose.setClassId(id);
        choose.setUserId(userid);

        Result result = classService.addChoose(choose);
//        String card= classService.getCardByid(id);

        return result;
    }

    @RequestMapping("/ToMyClass")
    public String ToMyClass(String className,Model model,HttpSession session) throws JsonProcessingException {

        User userInfo = (User) session.getAttribute("userInfo");
        List<Class> myCourseList=classService.queryMyClass(userInfo.getId(),className);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(myCourseList);
        model.addAttribute("myCourseList", json);
        return "myClass";
    }


    @RequestMapping("/DelChoose")
    @ResponseBody
    public Result DelChoose(Integer id, HttpSession session, Model model)throws JsonProcessingException{
        User userInfo = (User) session.getAttribute("userInfo");
        Integer userid = userInfo.getId();
        Choose choose = new Choose();
        choose.setClassId(id);
        choose.setUserId(userid);

        Result result = classService.delChoose(choose);
//        String card= classService.getCardByid(id);
        return result;
    }

    @RequestMapping("/ToAdminClass")
    public String ToEmployee(){
        return "adminClass";
    }

    @RequestMapping("/ToAdminClassInfo")
    @ResponseBody
    public Result ToEmployeeInfo(Model model, @ModelAttribute ClassQuery classQuery) throws JsonProcessingException {
        PageHelper.startPage(classQuery.getPage(),classQuery.getLimit());
        List<Class> classList=   classService.queryAllAdminClass( classQuery.getClassName());
        PageInfo<Class> pageInfo = new PageInfo<>(classList);
        List<Class> data = pageInfo.getList();
        int count =   classService.getAllCount();
        return  new Result(0,"",data,count);
    }

    @RequestMapping("/addAdminClass")
    @ResponseBody
    public Result addAdminClass(@ModelAttribute Class class1){
        Result result = classService.addAdminClass(class1);
        return result;
    }

    @RequestMapping("/delAdminClass")
    @ResponseBody
    public Result delMember(@RequestParam("ids[]") Integer[] ids){
        Result result = classService.delBatch(ids);
        return result;
    }

    @RequestMapping("/EditAdminClass")
    @ResponseBody
    public Result EditAdminClass(@RequestParam("id") Integer id, @RequestParam("field") String field, @RequestParam("value") String value){
//        转换，将userAge转换为user_age
        String columnName =   field.replaceAll("[A-Z]", "_$0").toLowerCase();    // 数据库中对应的列名
        Result result=classService.EditAdminClass(id,columnName,value);
        return result;
    }
}
